package de.gof.ppstool.staticdata.db;

import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import de.gof.ppstool.staticdata.StaticDataNotFoundException;

@Repository
@Transactional(propagation = Propagation.MANDATORY)
public class StaticDataVerwaltungDAO implements IStaticDataVerwaltungDAO,
		Serializable {
	//	private static final Log LOG = LogFactory.getLog(GofUtil.class);
	//	private static final boolean DEBUG = LOG.isDebugEnabled();
	//	private static final boolean TRACE = LOG.isDebugEnabled();
	private static final long serialVersionUID = -3324011039291149289L;
	@PersistenceContext
	private EntityManager em;

	/**
	 * Changes an existing StaticData Entry
	 */
	@Override
	public StaticData changeStaticData(StaticData sdata) {
		if (sdata == null)
			return sdata;

		sdata = em.merge(sdata);
		return sdata;
	}

	/**
	 * Find staticData by ID
	 * @throws StaticDataNotFoundException  
	 */
	@Override
	public StaticData findStaticData(Long sDataId)
			throws StaticDataNotFoundException {
		final StaticData sd = em.find(StaticData.class, sDataId);
		if (sd == null) {
			final StaticDataNotFoundException e = new StaticDataNotFoundException(
					sDataId);
			throw e;
		}

		return sd;
	}

}
